from hashlib import sha256
from PyQt5.QtWidgets import QWidget, QMessageBox
from res.ui import chgpass
from src.util.entity import 班主任
from src.util.mydb2 import mysession, rd


class chgpasswgt(QWidget):
    def __init__(self, parent):
        super().__init__()
        ui = chgpass.Ui_Form()
        ui.setupUi(self)
        self.ui = ui
        ui.pushButton.clicked.connect(self.chg)
        self.parent_ = parent

    def chg(self):
        ui = self.ui
        if ui.lineEdit_1.text() != ui.lineEdit_2.text():
            QMessageBox.information(self, "提示", "两次输入的密码不一致")
            return
        tinfo:班主任 = self.parent_.tinfo
        usr = tinfo.教师号
        pw = sha256(f"{tinfo.系统密码}".encode("utf-8")).hexdigest()
        rd.delete(f"{usr}+{pw}")
        print("已删除 Redis 缓存")
        passwd = ui.lineEdit_1.text()
        tinfo.系统密码 = sha256(passwd.encode("utf-8")).hexdigest()  # type: ignore
        mysession.commit()
        QMessageBox.information(self, "提示", "修改成功")
        self.hide()
